import json
import google.protobuf.json_format as json_format

from coin.exchange.base.kr_rest.private_client_base import PrivateClientBase
from coin.exchange.okex_futures.kr_rest.futures_product import OkexFuturesProduct
from coin.proto.coin_query_pb2 import (
    AccountBalance,
    AccountPosition
)
from coin.exchange.okex_futures_v5.kr_rest.native_private_client import (
    OkexFuturesNativePrivateClient)
from coin.exchange.okex_futures_v5.kr_rest.product_info import okex_futures_product_info    
from cc.appcoin2.support.order.py_parser import PyOrderParser
from coin2.exchange.order.system_pb2 import OrderSystemConfig
from coin2.exchange.symbology_pb2 import ProductConfig


class OkexFuturesPrivateClient(PrivateClientBase):
  ProductType = OkexFuturesProduct

  def __init__(self, key_file):
    super().__init__()
    self.nprivc = OkexFuturesNativePrivateClient(key_file=key_file)
    pi_bundle = okex_futures_product_info._product_infos
    assert pi_bundle.HasField('mea')
    oss_config = OrderSystemConfig(
        mea=pi_bundle.mea,
        products=ProductConfig(norms=[pi.symbol for pi in pi_bundle.product_infos]))
    self._parser = PyOrderParser.from_mea_str(pi_bundle.mea)
    self._parser.add_product(oss_config.SerializeToString())

  def query_account_balance_impl(self):
    update = self.nprivc.query_accounts()
    msg = self._parser.parse_account_balance(json.dumps(update.msg))
    account_balance = AccountBalance()
    json_format.Parse(msg, account_balance)
    update.msg = account_balance
    return update

  def query_account_position_impl(self):
    update = self.nprivc.query_positions()
    msg = self._parser.parse_account_position(json.dumps(update.msg))
    account_position = AccountPosition()
    json_format.Parse(msg, account_position)
    update.msg = account_position
    return update

  def query_list_orders_impl(self, product):
    raise NotImplementedError()

  def query_fills_impl(self, product):
    raise NotImplementedError()

  def query_submit_impl(self, product, side, price, qty):
    raise NotImplementedError()

  def query_cancel_impl(self, product, order_id):
    raise NotImplementedError()

  def query_cancel_product_impl(self, product):
    raise NotImplementedError()

  def query_account_fills_impl(self):
    raise NotImplementedError()

  def query_account_list_orders_impl(self):
    raise NotImplementedError()
